import React from 'react';
import { Popconfirm } from 'antd';
import pageTabUtil from '@/utils/pageTabUtil';
import { history } from 'umi'
import styles from './index.less';

const LeaveConfirm = () => (
    <div className={styles.leaveConfirm}>
        <p>确定离开此页面？</p>
        <span>离开后将会清空未暂存的内容哦</span>
    </div>
);

/**
 * @description 编辑取消确认信息，tab有历史记录返回历史记录，没有就关闭当前tab
 */
const CancelEditConfirm = ({ leaveConfirm, children, linkUrl, ...rest }) => (
    <Popconfirm
        title={leaveConfirm ? <LeaveConfirm /> : <>当前页面信息暂未保存，确定离开此页面？</>}
        arrowPointAtCenter
        onConfirm={() => {
            const tabKey = pageTabUtil.getCurrentKey();
            const hasHistory = pageTabUtil.getTabHistoryStatus(tabKey, -1);
            if (linkUrl) {
                history.replace(linkUrl);
            } else if (hasHistory) {
                pageTabUtil.goBack(-1);
            } else {
                pageTabUtil.closeTab();
            }
        }}
        okText="确定"
        cancelText="取消"
        {...rest}
    >
        {children}
    </Popconfirm>
);

export default CancelEditConfirm;
